package com.sy.simpleegg.util

import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText

object SoftInputUtil {


    fun showSoftInput(view: View?) {
        if (view == null) return
        val inputMethodManager =
            view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.showSoftInput(view, 0)
    }

    fun hideSoftInput(view: View?) {
        if (view == null) return
        val inputMethodManager =
            view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }

    fun isHideKeyboard(view: View?, event: MotionEvent): Boolean{
        if (view != null && view is EditText){
            val location = intArrayOf(0, 0)
            view.getLocationInWindow(location)
            //获取焦点控件view的未知，
            val left = location[0]
            val top = location[1]
            val right = left + view.width
            val bottom = top + view.height
            //判断点击坐标是否在view的范围内
            val inViewZone = (event.x > left && event.x < right && event.y > top && event.y < bottom)
            return inViewZone
        }
        return false
    }
}